package com.my.admin.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.admin.annotation.CleanCache;
import com.my.admin.entity.Menu;
import com.my.admin.service.MenuService;
import com.my.admin.util.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class MenuController {

    @Autowired
    private MenuService service;

    @GetMapping("menu/get")
    public Rest get() {
        return Rest.ok().setData("tree", TreeUtil.build(service.getList(), 0L, ((menu, tree) -> {
            tree.setParentId(menu.getParentId());
            tree.setId(menu.getId());
            tree.setName(menu.getName());
            tree.putExtra("deep", menu.getDeep());
            tree.putExtra("icon", menu.getIcon());
            tree.putExtra("url", menu.getUrl());
            tree.putExtra("code", menu.getCode());
            tree.putExtra("sort", menu.getSort());
            tree.putExtra("parentName", menu.getParentName() == null ? "目录" : menu.getParentName());
            tree.putExtra("createTime", DateUtil.format(menu.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
            tree.putExtra("updateTime", DateUtil.format(menu.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
        })));
    }

    @PostMapping("menus")
    public Rest add(@RequestBody Menu menu) {
        if(service.save(menu)) {
            return Rest.ok("添加成功！");
        }else {
            return Rest.err("添加失败!");
        }
    }

    @PutMapping("menus")
    @CleanCache(value = "com.my.admin.service.impl.MenuServiceImpl:getMenuByUserId:")
    public Rest edit(@RequestBody Menu menu) {
        if(service.updateById(menu)) {
            return Rest.ok("修改成功！");
        }else {
            return Rest.err("修改失败！");
        }
    }

    @DeleteMapping("menus")
    @CleanCache(value = "com.my.admin.service.impl.MenuServiceImpl:getMenuByUserId:")
    public Rest del(@RequestParam long id) {
        if(!service.list(new QueryWrapper<Menu>().eq("parent_id", id)).isEmpty()) {
            return Rest.err("无法删除，此节点下还有子节点！");
        }
        if(service.removeById(id)) {
            return Rest.ok("删除成功！");
        }else {
            return Rest.err("删除失败！");
        }
    }

    @GetMapping("menu/tree")
    public Rest tree() {
        List<Menu> list = new ArrayList<>();
        list.add(new Menu().setId(0L).setParentId(-1L).setName("目录").setDeep(-1));
        list.addAll(service.list());
        return Rest.ok().setData("tree", TreeUtil.build(list, -1L, ((menu, tree) -> {
            tree.setId(menu.getId());
            tree.setName(menu.getName());
            tree.setParentId(menu.getParentId());
            tree.putExtra("title", menu.getName());
            tree.putExtra("expand", menu.getDeep() < 2);
            tree.putExtra("deep", menu.getDeep());
        })));
    }

}
